<?php
/**
 * Created by IntelliJ IDEA.
 * User: Maxwell
 */

namespace Home\Org\Workflow;
require 'vendor/autoload.php';

//Symfony Workflow
use Symfony\Component\Workflow\DefinitionBuilder;
use Symfony\Component\Workflow\Transition;
use Symfony\Component\Workflow\Workflow;
use Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore;
use Symfony\Component\Workflow\Registry;

//Related to Model
use Home\Model\AuthModel;

//Dumper Export Workflow
use Symfony\Component\Workflow\Dumper\GraphvizDumper;

/**
 * Created by PhpStorm.
 * Singleton Object
 * 供应商授权
 */
class SupplierAuthWorkflow
{
    static private $_instance = null;

    public $definition;
    public $workflow;
    public $registry;

    private function __construct()
    {
        //Create DefinitionBuilder
        $this->builder = new DefinitionBuilder();
        /**
         * Places
         * 申请 = apply
         * 通过 = approved
         * 拒绝 = rejected
         */
        $this->builder->addPlaces(['apply', 'approved', 'rejected']);

        //Set Transition
        $this->builder->addTransition(new Transition('to_approved', 'apply', 'approved'));
        $this->builder->addTransition(new Transition('reject_go', 'apply', 'rejected'));

        //Create Definition
        $this->definition = $this->builder->build();

        //Set State tag && Create Marking Store
        $marking = new MultipleStateMarkingStore('currentPlace');

        //Create workflow
        $this->workflow = new Workflow($this->definition, $marking);

        //Register Workflow to Model
        $this->registry = new Registry();
        $this->registry->add($this->workflow, AuthModel::class);

    }

    static public function getInstance()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new SupplierAuthWorkflow();
        }
        return self::$_instance;
    }

    public function dumperIMG(){
        //生成图片
        $dumper = new GraphvizDumper();
        echo $dumper->dump($this->definition);
    }
}